<?php
declare (strict_types = 1);

namespace app\admin\middleware;

class Check
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $url=$request->url();
        $ip=$request->ip();
        $regex = "/\/login/i";
        if(preg_match($regex, $url)){
            return  $next($request);
        }
        $token = (string)$request->header('Authorization');
 
        $jwtData = app('auth')->decodeToken($token);
  
        if ($jwtData['status'] != 1001) {
            $res = ['code' => 0, 'msg' => $jwtData['data'], 'data' => '', 'token' => '', 'url' => '/login'];
            die(json_encode($res));
        }


        if($jwtData['data']->local!=$ip){
            $res = ['code' => 0, 'msg' => '我们己获取您的行为及信息，如继续，我们将采取措施', 'data' => '', 'token' => '', 'url' => '/login'];
            die(json_encode($res));
        }

        $request->uid = $jwtData['data']->adminId;
        return $next($request);
    }
}
